package com.campusbackend.service.impl;

import com.campusbackend.entity.ProjectExperiences;
import com.campusbackend.entity.vo.ProjectCreateVO;
import com.campusbackend.entity.vo.ProjectUpdateVO;
import com.campusbackend.mapper.ProjectExperiencesMapper;
import com.campusbackend.service.ProjectExperiencesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 项目经历表 服务实现类
 * </p>
 *
 * @author yezi
 * @since 2025-07-24
 */
@Service
public class ProjectExperiencesServiceImpl extends ServiceImpl<ProjectExperiencesMapper, ProjectExperiences> implements ProjectExperiencesService {

    @Autowired
    private ProjectExperiencesMapper projectExperiencesMapper;

    @Override
    public List<ProjectExperiences> getTeacherProjects(Long teacherId) {
        return projectExperiencesMapper.selectByUserTypeAndUserId((byte) 1, teacherId);
    }

    @Override
    public String addTeacherProject(Long teacherId, ProjectCreateVO createVO) {
        try {
            ProjectExperiences project = new ProjectExperiences();
            BeanUtils.copyProperties(createVO, project);
            
            // 设置用户信息
            project.setUserType((byte) 1); // 1-教师
            project.setUserId(teacherId);
            
            // 设置时间
            project.setCreateTime(LocalDateTime.now());
            project.setUpdateTime(LocalDateTime.now());
            
            boolean saved = save(project);
            if (saved) {
                return "项目经历添加成功";
            } else {
                return "项目经历添加失败，请稍后重试";
            }
        } catch (Exception e) {
            return "添加项目经历时发生错误：" + e.getMessage();
        }
    }

    @Override
    public String updateTeacherProject(Long teacherId, ProjectUpdateVO updateVO) {
        try {
            // 首先检查该项目记录是否属于当前教师
            ProjectExperiences existingProject = projectExperiencesMapper.selectByIdAndUser(updateVO.getId(), (byte) 1, teacherId);
            if (existingProject == null) {
                return "项目记录不存在或无权限访问";
            }
            
            // 更新项目信息
            BeanUtils.copyProperties(updateVO, existingProject, "userType", "userId", "createTime");
            existingProject.setUpdateTime(LocalDateTime.now());
            
            boolean updated = updateById(existingProject);
            if (updated) {
                return "项目经历更新成功";
            } else {
                return "项目经历更新失败，请稍后重试";
            }
        } catch (Exception e) {
            return "更新项目经历时发生错误：" + e.getMessage();
        }
    }

    @Override
    public String deleteTeacherProject(Long teacherId, Long projectId) {
        try {
            // 首先检查该项目记录是否属于当前教师
            ProjectExperiences existingProject = projectExperiencesMapper.selectByIdAndUser(projectId, (byte) 1, teacherId);
            if (existingProject == null) {
                return "项目记录不存在或无权限访问";
            }
            
            boolean deleted = removeById(projectId);
            if (deleted) {
                return "项目经历删除成功";
            } else {
                return "项目经历删除失败，请稍后重试";
            }
        } catch (Exception e) {
            return "删除项目经历时发生错误：" + e.getMessage();
        }
    }

    @Override
    public List<ProjectExperiences> getStudentProjects(Long studentId) {
        return projectExperiencesMapper.selectByUserTypeAndUserId((byte) 2, studentId);
    }
} 